package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.Cart;
import com.neusoft.po.CommonResult;
import com.neusoft.service.CartService;

@RefreshScope
@RestController
@RequestMapping("/CartController")
public class CartController {
	@Autowired
	CartService cartService;
	
	@PostMapping("/saveCart/{userId}/{businessId}/{foodId}")
	public CommonResult<Integer> saveCart(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") int businessId,
			@PathVariable("foodId") int foodId)
	{
		Cart cart=new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		return new CommonResult<Integer>(200,"success",cartService.saveCart(cart));
	}
	
	@GetMapping("/listCart/{userId}/{businessId}")
	public CommonResult<List<Cart>> listCart(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") int businessId)
	{
		Cart cart=new Cart();
		
		cart.setBusinessId(businessId);
		cart.setUserId(userId);
		return new CommonResult<List<Cart>>(200,"success",cartService.listCart(cart));
	}

	
	
	@PutMapping("/updateCart/{userId}/{businessId}/{foodId}/{quantity}")
	public CommonResult<Integer> updateCart(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") int businessId,
			@PathVariable("foodId") int foodId,
			@PathVariable("quantity") int quantity)
	{
		Cart cart=new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		cart.setQuantity(quantity);
		return new CommonResult<Integer>(200,"success",cartService.updateCart(cart));
	}
	
	@DeleteMapping("/removeCart/{userId}/{businessId}/{foodId}")
	public CommonResult<Integer> removeCart(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") int businessId,
			@PathVariable("foodId") int foodId)
	{
		Cart cart=new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		return new CommonResult<Integer>(200,"success",cartService.removeCart(cart));
	}
}
